package com.newhonor.springboot_shiro_redis.service.impl;

import com.newhonor.springboot_shiro_redis.dao.UserDAO;
import com.newhonor.springboot_shiro_redis.entity.User;
import com.newhonor.springboot_shiro_redis.service.UserService;
import com.newhonor.springboot_shiro_redis.utils.SaltUtils;
import org.apache.shiro.crypto.hash.Md2Hash;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author newhonor
 * @Date 2021/1/22 21:41
 * @Version 1.0
 **/
//指定名字，realm用到
@Service("userService")
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDAO userDAO;

    @Override
    public void register(User user) {
        //参数1：明文， 参数2：salt， 参数3：hash散列
        String salt = SaltUtils.getSalt(8);
        user.setSalt(salt);
        Md5Hash md5Hash = new Md5Hash(user.getPassword(), salt, 1024);
        user.setPassword(md5Hash.toHex());

        userDAO.save(user);
    }

    @Override
    public User findByUserName(String username) {
        return userDAO.findByUserName(username);
    }
}
